package com.tau.iplayer.client.dialogs;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.gadgets.client.gwtrpc.GadgetsGwtRpc;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.tau.iplayer.client.DataStoreService;
import com.tau.iplayer.client.DataStoreServiceAsync;
import com.tau.iplayer.client.IPlayer;
import com.tau.iplayer.client.IPlayerUtils;
import com.tau.iplayer.client.models.Playlist;
import com.tau.iplayer.client.models.Track;
import com.tau.iplayer.client.models.TrackTransaction;

public class IncomingTrackDialog extends DialogBox {
	
	private static final boolean IS_AUTO_HIDE = false;
	private static final boolean IS_MODAL = false;
	private static final boolean IS_ANIMATED = true;
	private static final boolean IS_GLASS = false;
	private static final String HEADER = "Incoming Track";
	private TrackTransaction transaction;
	private TextBox titleTextBox;
	private TextBox artistTextBox;
	private TextArea textArea;
	private ListBox playlistListBox;
	private IPlayer iPlayer;
	private String senderName;

	public IncomingTrackDialog(TrackTransaction transaction, IPlayer iPlayer, String senderName) {
		this.transaction = transaction;
		this.iPlayer = iPlayer;
		this.senderName = senderName;
		setAutoHideEnabled(IS_AUTO_HIDE);
		setModal(IS_MODAL);
		setAnimationEnabled(IS_ANIMATED);
		setGlassEnabled(IS_GLASS);
		setText(HEADER);
		createContents();
	}

	private void createContents() {
		DockPanel dockPanel = new DockPanel();

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		final Button addButton = new Button("Add Song");
		addButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				handleAddClicked();
			}
		});
		addButton.setStyleName("button");
		addButton.setEnabled(true);
		Button previewButton = new Button("Preview");
		previewButton.setStyleName("button");
		previewButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				handlePreviewClick();
			}
		});
		Button cancelButton = new Button("Cancel");
		cancelButton.setStyleName("button");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleCancelClick();
			}
		});
		horizontalPanel.add(addButton);
		horizontalPanel.add(previewButton);
		horizontalPanel.add(cancelButton);
		horizontalPanel.setSpacing(3);
		dockPanel.add(horizontalPanel, DockPanel.SOUTH);
		dockPanel.setCellHorizontalAlignment(horizontalPanel,
				HasHorizontalAlignment.ALIGN_CENTER);
		
		Grid msgGrid = new Grid(1,2);
		msgGrid.setText(0, 0, "Message:");
		msgGrid.getCellFormatter().setStyleName(0, 0, "standardLabel");
		textArea = new TextArea();
		textArea.setStyleName("addSongDialogTextBox");
		textArea.setReadOnly(true);
		textArea.setValue(transaction.getMessage());
		msgGrid.setWidget(0, 1, textArea);
		dockPanel.add(msgGrid, DockPanel.SOUTH);

		Grid formGrid = new Grid(5, 2);
		formGrid.setText(0, 0, "From:");
		formGrid.getCellFormatter().setStyleName(0, 0, "standardLabel");
		formGrid.setText(0, 1, senderName);
		formGrid.getCellFormatter().setStyleName(0, 1, "standardLabel");

		formGrid.setText(1, 0, "Title:");
		formGrid.getCellFormatter().setStyleName(1, 0, "standardLabel");
		titleTextBox = new TextBox();
		titleTextBox.setText(transaction.getTitle());
		titleTextBox.setStyleName("addSongDialogTextBox");
		titleTextBox.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				checkRestrications(addButton);
			}
		});
		formGrid.setWidget(1, 1, titleTextBox);

		formGrid.setText(2, 0, "Artist:");
		formGrid.getCellFormatter().setStyleName(2, 0, "standardLabel");
		artistTextBox = new TextBox();
		if (transaction.getArtist() != null && transaction.getArtist().length() > 0) {
			artistTextBox.setText(transaction.getArtist());
		}
		artistTextBox.setStyleName("addSongDialogTextBox");
		formGrid.setWidget(2, 1, artistTextBox);

		formGrid.setText(3, 0, "Playlist:");
		formGrid.getCellFormatter().setStyleName(3, 0, "standardLabel");
		playlistListBox = new ListBox();
		for (Playlist playlist : iPlayer.getPlaylists()) {
			playlistListBox.addItem(playlist.getName());
		}
		playlistListBox.setStyleName("standardLabel");
		playlistListBox.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				checkRestrications(addButton);
			}
		});
		Playlist currentPlaylist = iPlayer.getCurrentPlaylist();
		if (currentPlaylist != null) {
			int i = iPlayer.getPlaylistListBoxIndex(playlistListBox,
					currentPlaylist);
			if (i != -1) {
				playlistListBox.setItemSelected(i, true);
			}
		}
		formGrid.setWidget(3, 1, playlistListBox);
		
		formGrid.setText(4, 0, "Duration:");
		formGrid.getCellFormatter().setStyleName(4, 0, "standardLabel");
		formGrid.setText(4, 1, IPlayerUtils.formatTime(transaction.getDuration()));
		formGrid.getCellFormatter().setStyleName(4, 1, "standardLabel");
		dockPanel.add(formGrid, DockPanel.WEST);
		
		Image thumbnailImage = new Image(transaction.getThumbnailURL());
		dockPanel.add(thumbnailImage, DockPanel.CENTER);

		setWidget(dockPanel);
		thumbnailImage.addLoadHandler(new LoadHandler() {

			@Override
			public void onLoad(LoadEvent event) {
				handleThumbnailOnLoad();
			}
		});
	}

	private void handlePreviewClick() {
		Track track = new Track(transaction.getArtist(), transaction.getArtist(), transaction.getDuration(), transaction.getVideoID(), transaction.getThumbnailURL(), 0);
		iPlayer.loadTrack(track, true);
	}

	private void handleThumbnailOnLoad() {
		center();
	}

	private void handleCancelClick() {
		this.hide();

	}

	private void handleAddClicked() {
		DataStoreServiceAsync dataStoreService = GWT
				.create(DataStoreService.class);
		GadgetsGwtRpc.redirectThroughProxy((ServiceDefTarget) dataStoreService);
		Track track = new Track(titleTextBox.getValue(), artistTextBox.getValue(), transaction.getDuration(), transaction.getVideoID(), transaction.getThumbnailURL(), 0);
		dataStoreService.addTrack(transaction.getReceiverID(), track, new Playlist(playlistListBox.getValue(playlistListBox.getSelectedIndex())), new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
		this.hide();
	}

	/**
	 * @param button
	 */
	private void checkRestrications(final Button button) {
		boolean isEnabled = !IPlayerUtils.isEmpty(titleTextBox
				.getValue()) && (playlistListBox.getSelectedIndex() != -1);
		button.setEnabled(isEnabled);
	}

}
